package leetcode;

public class FindMedianSortedArrays {
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        final int nums1Length = nums1.length;
        final int nums2Length = nums2.length;
        final int allLength = nums1Length + nums2Length;
        int[] l = new int[allLength];
        int key1 = 0;
        int key2 = 0;
        for (int i = 0; i < allLength; i++){
            if (key1 == nums1Length){
                l[i] = nums2[key2];
                key2++;
            }else if (key2 == nums2Length){
                l[i] = nums1[key1];
                key1++;
            }else {
                if (nums1[key1] < nums2[key2]){
                    l[i] = nums1[key1];
                    key1++;
                }else if (nums1[key1] > nums2[key2]){
                    l[i] = nums2[key2];
                    key2++;
                }else {
                    l[i] = nums1[key1];
                    l[++i] = nums2[key2];
                    key1++;
                    key2++;
                }
            }
        }
        if (allLength % 2 == 0){
            return (Double.valueOf(l[allLength/2-1]) + Double.valueOf(l[allLength/2]))/2;
        }else {
            return l[allLength/2];
        }
    }

    public static void main(String[] args){
        int [] a = {1, 3};
        int [] b = {2, 5};

        System.out.println(findMedianSortedArrays(a,b));
    }
}
